package hhm.android.library.weightHeightPicker.heightPicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import hhm.android.library.datePicker.view.WheelView;
import hhm.android.library.utils.GetStringListener;

public class HeightPickerView extends LinearLayout {

    final HeightPick mDatePick = new HeightPick();
    private GetStringListener mOnChangeListener;
    private String type = "CM";
    private BaseHeightAdapter inAdapter;
    private WheelView mInView;

    public HeightPickerView(Context context) {
        super(context);
    }

    public HeightPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HeightPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnChangeListener(GetStringListener listener) {
        this.mOnChangeListener = listener;
    }

    public void show(String type, double initData, @NonNull HeightParams params, int selectColor) {
        this.type = type;

        setInitData(initData);

        if (params.types == null) {
            return;
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        for (int t : params.types) {

            WheelView wheelView = new WheelView(getContext());
            final BaseHeightAdapter datePickAdapter;

            switch (t) {
                case HeightParams.TYPE_CM:
                    wheelView.setCyclic(false);
                    datePickAdapter = new CMAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.cm = datePickAdapter.getValue(newValue);
                        HeightPickerView.this.onChanged();
                    });
                    break;
                case HeightParams.TYPE_MM:
                    wheelView.setCyclic(true);
                    datePickAdapter = new MMAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.mm = datePickAdapter.getValue(newValue);
                        HeightPickerView.this.onChanged();
                    });
                    break;
                case HeightParams.TYPE_FT:
                    wheelView.setCyclic(false);
                    datePickAdapter = new FTAdapter(params, mDatePick, selectColor);
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.cm = datePickAdapter.getValue(newValue);
                        if (inAdapter != null) {
                            inAdapter.refreshValues();
                        }
                        if (mInView != null) {
                            mInView.setCurrentItem(inAdapter.getCurrentIndex());
                        }
                        HeightPickerView.this.onChanged();
                    });
                    break;
                case HeightParams.TYPE_IN:
                    mInView = wheelView;
                    wheelView.setCyclic(true);
                    datePickAdapter = new INAdapter(params, mDatePick, selectColor);
                    inAdapter = datePickAdapter;
                    wheelView.setAdapter(datePickAdapter);
                    wheelView.setCurrentItem(datePickAdapter.getCurrentIndex());
                    layoutParams.weight = 2;
                    wheelView.addChangingListener((wheel, oldValue, newValue) -> {
                        mDatePick.mm = datePickAdapter.getValue(newValue);
                        HeightPickerView.this.onChanged();
                    });
                    break;
            }

            addView(wheelView, layoutParams);
        }
    }

    private void onChanged() {
        if (mOnChangeListener != null) {
            mOnChangeListener.getString(getChildHeight());
        }
    }

    public String getChildHeight() {
        int cm = mDatePick.cm;
        int mm;
        if (type.equals("FT") && mDatePick.mm / 10 == 0) {
            mm = mDatePick.mm;
            return cm + ".0" + mm;
        } else {
            mm = mDatePick.mm;
            return cm + "." + mm;
        }

    }

    public void setInitData(double initData) {
        if (initData == -1 || initData == 0) {
            if (type.equals("CM")) {
                mDatePick.setData(20, 0);
            } else {
                mDatePick.setData(0, 8);
            }
        } else {
            String data = String.valueOf(initData);
            int big;
            int small;
            if (data.contains(".")) {
                String[] split = data.split("\\.");
                big = Integer.parseInt(split[0]);
                if (!split[1].startsWith("0") && split[1].length() == 1 && type.equals("FT")) {
                    small = Integer.parseInt(split[1]) * 10;
                } else {
                    small = Integer.parseInt(split[1]);
                }
            } else {
                big = Integer.parseInt(data);
                small = 0;
            }
            mDatePick.setData(big, small);
        }
    }
}
